<?php
/**
 * Created by PhpStorm.
 * User: yangchunrun
 * Date: 17/3/21
 * Time: 下午8:21
 */

if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}
require_once 'setting.class.php';
WxSetting::showResource();
$identifier=$_GET['identifier'];
define('RSELF', 'action=plugins&operation=config&identifier='.$identifier.'&pmod=reply&mact=');
if ($_GET['mact'] == 'add') {

    if (submitcheck('submit')) {
        $keyword = isset($_GET['keyword']) ? addslashes($_GET['keyword']) : '';
        $text = isset($_GET['text']) ? addslashes($_GET['text']) : '';
        $parent_id = $_GET['parent_id']>0?$_GET['parent_id']:0;
        $return_url = 'action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=list';
        if($parent_id>0){
            $return_url = "action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id=".$parent_id;
        }
        $insertData = array();
        $insertData['parent_id']=$parent_id;
        $insertData['keyword'] = $keyword;
        $insertData['text'] = $text;
        $insertData['status'] = $_GET['status'];
        $insertData['is_match'] = $_GET['is_match'];
        $insertData['order_num'] = $_GET['order_num'];
        C::t('#thinker_wx#thinker_wx_keyword')->insert($insertData);
        cpmsg(lang('plugin/thinker_wx','reply_act_success'), $return_url, 'succeed');
    } else {
        $parent_id = (int)$_GET['parent_id'];
        $return_url  = 'admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=list';
        if($parent_id) {
            $row = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_id($parent_id);
            $return_url = 'admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id='.$parent_id;
        }

        showformheader('plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=add');
        showtableheader();
        echo '<tr><th colspan="15" class="partition"><a href="'.$return_url.'"><font color="#F60"><b>' . lang('plugin/thinker_wx','reply_list_back') . '</b></font></a></th></tr>';

        if($parent_id) {
            echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_parent') . '</th><th>'.$row['keyword'].'<input type="hidden" name="parent_id" value="'.$parent_id.'"/></th></tr>';

        }else {
            echo '<tr class="header"><th>' . lang('plugin/thinker_wx', 'reply_keyword') . '</th><th></th></tr>';
            echo '<tr><td width="300"><input name="keyword" type="text" value="" size="40" /></td></tr>';
        }

        echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_text') . '</th><th></th></tr>';
        echo '<tr><td><textarea rows="6" id="res_text" name="text" cols="40" class="tarea"></textarea></td>
<td><a href="javascript:;" id="rsel" onclick="showResource(\'res_text\')">选择素材</a></td>
</tr>';

        echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_status') . '</th><th></th></tr>';
        echo '<tr><td width="300"><select name="status"><option value="0">不启用</option><option value="1">启用</option></select></td></tr>';

        if($parent_id==0) {
            echo '<tr class="header"><th>' . lang('plugin/thinker_wx', 'reply_match') . '</th><th></th></tr>';
            echo '<tr><td width="300"><select name="is_match"><option value="0">不启用</option><option value="1">启用</option></select></td></tr>';
        }

        echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_order_num') . '</th><th></th></tr>';
        echo '<tr><td width="300"><input name="order_num" type="text" value="" size="40" /></td></tr>';

        showsubmit('submit', 'submit');
        showtablefooter();
        showformfooter();
    }

}else if($_GET['mact'] == 'edit'){
    $replyInfo = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_id($_GET['id']);
    if(submitcheck('submit')){
        $keyword = isset($_GET['keyword']) ? addslashes($_GET['keyword']) : '';
        $text = isset($_GET['text']) ? addslashes($_GET['text']) : '';
        $parent_id = $_GET['parent_id']>0?$_GET['parent_id']:0;
        $return_url = 'action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=list';
        if($parent_id>0){
            $return_url = "action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id=".$parent_id;
        }
        $updateData = array();
        $updateData['parent_id']=$parent_id;
        $updateData['keyword'] = $keyword;
        $updateData['text'] = $text;
        $updateData['status'] = $_GET['status'];
        $updateData['is_match'] = $_GET['is_match'];
        $updateData['order_num'] = $_GET['order_num'];
        C::t('#thinker_wx#thinker_wx_keyword')->update($replyInfo['id'],$updateData);

        cpmsg(lang('plugin/thinker_wx','reply_act_success'), $return_url, 'succeed');
    }else{
        $parent_id = $replyInfo['parent_id'];
        $return_url  = 'admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=list';
        if($parent_id) {
            $row = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_id($parent_id);
            $return_url = 'admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id='.$parent_id;
        }

        showformheader('plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=edit&id='.$_GET['id']);
        showtableheader();
        echo '<tr><th colspan="15" class="partition"><a href="'.$return_url.'"><font color="#F60"><b>' . lang('plugin/thinker_wx','reply_list_back') . '</b></font></a></th></tr>';

        if($parent_id) {
            echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_parent') . '</th><th>'.$row['keyword'].'<input type="hidden" name="parent_id" value="'.$parent_id.'"/></th></tr>';

        }else {
            echo '<tr class="header"><th>' . lang('plugin/thinker_wx', 'reply_keyword') . '</th><th></th></tr>';
            echo '<tr><td width="300"><input name="keyword" type="text" value="' . $replyInfo['keyword'] . '" size="40" /></td></tr>';
        }

        echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_text') . '</th><th></th></tr>';
        echo '<tr><td><textarea rows="6" id="res_text" name="text" cols="40" class="tarea">'.$replyInfo['text'].'</textarea></td>
<td><a href="javascript:;" id="rsel" onclick="showResource(\'res_text\')">选择素材</a></td>
</tr>';

        echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_status') . '</th><th></th></tr>';
        echo '<tr><td width="300"><select name="status"><option value="0" '.($replyInfo['status']==0?'selected':'').'>不启用</option><option  value="1" '.($replyInfo['status']==1?'selected':'').'>启用</option></select></td></tr>';

        if($parent_id==0) {
            echo '<tr class="header"><th>' . lang('plugin/thinker_wx', 'reply_match') . '</th><th></th></tr>';
            echo '<tr><td width="300"><select name="is_match"><option value="0" ' . ($replyInfo['is_match'] == 0 ? 'selected' : '') . '>不启用</option><option value="1" ' . ($replyInfo['is_match'] == 1 ? 'selected' : '') . '>启用</option></select></td></tr>';
        }

        echo '<tr class="header"><th>' . lang('plugin/thinker_wx','reply_order_num') . '</th><th></th></tr>';
        echo '<tr><td width="300"><input name="order_num" type="text" value="'.$replyInfo['order_num'].'" size="40" /></td></tr>';
        showsubmit('submit', 'submit');
        showtablefooter();
        showformfooter();
    }
}else if($_GET['formhash'] == FORMHASH && $_GET['mact'] == 'del'){
    $replyInfo = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_id($_GET['id']);
    $parent_id = $replyInfo['parent_id'];
    C::t('#thinker_wx#thinker_wx_keyword')->delete_by_id($_GET['id']);
    $return_url = 'action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=list';
    if($parent_id>0){
        $return_url = "action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id=".$parent_id;
    }
    cpmsg(lang('plugin/thinker_wx','reply_act_success'), $return_url, 'succeed');

} else if($_GET['mact'] == 'children') {
    $parent_id = (int)$_GET['id'];
    $row=C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_id($parent_id);
    $ppp = 10;
    $page = max(1, $_GET['page']);
    $start = ($page - 1) * $ppp;
    $count =C::t('#thinker_wx#thinker_wx_keyword')->count_by_where("parent_id={$parent_id}");
    $replyList = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_where("parent_id={$parent_id}", $start, $ppp);
    $multi = multi($count, $ppp, $page, ADMINSCRIPT."?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id=".$parent_id);
    showtableheader();

    echo '<tr><th colspan="15" class="partition">['.$row['keyword'] .']'. lang('plugin/thinker_wx', 'reply_list_children') . '</th></tr>';
    echo '<tr><th colspan="15">';
    echo '&nbsp;&nbsp;<a class="addtr" href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=add&parent_id='.$parent_id.'">' . lang('plugin/thinker_wx', 'reply_add_children') . '</a>';
    echo '</th></tr>';
    echo '<tr class="header">';

    echo '<th>' . lang('plugin/thinker_wx','reply_text') . '</th>';
    echo '<th>' . lang('plugin/thinker_wx','reply_status') . '</th>';
    echo '<th>' . lang('plugin/thinker_wx','handle') . '</th>';
    echo '</tr>';

    foreach ($replyList as $key => $value) {
        $status= $value['status']==1?lang('plugin/thinker_wx','on'):lang('plugin/thinker_wx','off');
        $is_match= $value['is_match']==1?lang('plugin/thinker_wx','on'):lang('plugin/thinker_wx','off');
        echo '<tr>';

        echo '<td>' . $value['text'] . '</td>';
        echo '<td>' . $status .'</td>';
        echo '<td>';
        echo '<a href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=edit&id=' . $value['id'] . '&formhash=' . FORMHASH . '">' . lang('plugin/thinker_wx','edit') . '</a>&nbsp;|&nbsp;';
        echo '<a href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=del&id=' . $value['id'] . '&formhash=' . FORMHASH . '">' . lang('plugin/thinker_wx','delete') . '</a>';
        echo '</td>';
        echo '</tr>';
    }
    showtablefooter();
    echo '<br style="clear:both"><div class="right pg">'.$multi.'</div>';

} else {
    $ppp = 10;
    $page = max(1, $_GET['page']);
    $start = ($page - 1) * $ppp;
    $count =C::t('#thinker_wx#thinker_wx_keyword')->count_by_where('parent_id=0');
    $replyList = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_where('parent_id=0', $start, $ppp);
    $multi = multi($count, $ppp, $page, ADMINSCRIPT.'?'.RSELF.'&filter='.$filter);
    showtableheader();

    echo '<tr><th colspan="15" class="partition">' . lang('plugin/thinker_wx', 'reply_list_title') . '</th></tr>';
    echo '<tr><th colspan="15">';
    echo '&nbsp;&nbsp;<a class="addtr" href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=add">' . lang('plugin/thinker_wx', 'reply_add') . '</a>';
    echo '</th></tr>';
    echo '<tr class="header">';
    echo '<th>' . lang('plugin/thinker_wx','reply_keyword') . '</th>';
    echo '<th>' . lang('plugin/thinker_wx','reply_text') . '</th>';
    echo '<th>' . lang('plugin/thinker_wx','reply_status') . '</th>';
    echo '<th>' . lang('plugin/thinker_wx','reply_match') . '</th>';
    echo '<th>' . lang('plugin/thinker_wx','handle') . '</th>';
    echo '</tr>';

    foreach ($replyList as $key => $value) {
        $count =C::t('#thinker_wx#thinker_wx_keyword')->count_by_where('parent_id='.$value['id']);
        $status= $value['status']==1?lang('plugin/thinker_wx','on'):lang('plugin/thinker_wx','off');
        $is_match= $value['is_match']==1?lang('plugin/thinker_wx','on'):lang('plugin/thinker_wx','off');
        echo '<tr>';
        echo '<td>' . $value['keyword'] . '</td>';
        echo '<td>' . $value['text'] . '</td>';
        echo '<td>' . $status .'</td>';
        echo '<td>' . $is_match .'</td>';
        echo '<td>';
        if($count>0) {
            echo '<a href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=children&id=' . $value['id'] . '&formhash=' . FORMHASH . '">' . lang('plugin/thinker_wx', 'reply_children') . '</a>&nbsp;|&nbsp;';
        }
        echo '<a href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=edit&id=' . $value['id'] . '&formhash=' . FORMHASH . '">' . lang('plugin/thinker_wx','edit') . '</a>&nbsp;|&nbsp;';
        echo '<a href="admin.php?action=plugins&operation=config&identifier=thinker_wx&pmod=reply&mact=del&id=' . $value['id'] . '&formhash=' . FORMHASH . '">' . lang('plugin/thinker_wx','delete') . '</a>';
        echo '</td>';
        echo '</tr>';
    }
    showtablefooter();
    echo '<br style="clear:both"><div class="right pg">'.$multi.'</div>';
}

?>